import {SysMenuAdd, SysMenuData, SysMenuDetail, SysMenuParams} from "@/pages/system/menu/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";


export const sysMenuPageList = async (sysMenuParams: SysMenuParams) => {
  return request<{
    data: SysMenuData[],
    success: boolean
  }>('/api/sys/menu/list', {
    method: 'POST',
    data: sysMenuParams
  });
}

export const buttonAuthorizeList = async (ids: (number | string)[]) => {
  return request<{
    data: SysMenuData[],
    success: boolean
  }>('/api/sys/menu/button/authorize/list', {
    method: 'POST',
    data: {
      ids
    }
  });
}


export const sysMenuUpdate = async (sysMenuAdd: SysMenuAdd) => {
  return request<{
    data: string;
  }>('/api/sys/menu/update', {
    method: 'POST',
    data: sysMenuAdd
  });
}

export const sysMenuAdd = async (sysMenuAdd: SysMenuAdd) => {
  return request<{
    data: string;
  }>('/api/sys/menu/add', {
    method: 'POST',
    data: sysMenuAdd
  });
}

export const sysMenuRemove = async (ids: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/sys/menu/remove', {
    method: 'POST',
    data: {
      ids
    }
  });
}

export const sysMenuDetail = async (id: number) => {
  return request<{
    data: SysMenuDetail;
  }>('/api/sys/menu/detail', {
    method: 'POST',
    data: {
      id
    }
  });
}

export const getRoutes = async () => {
  return await request<{
    data: {}[];
  }>('/api/sys/menu/getRoute', {
    method: 'POST',
  });
}

export const sysMenuImport = async (formData: FormData) => {
  return request<{
    data: string;
  }>('/api/sys/menu/import', {
    method: 'POST',
    data: formData,
    requestType: "form"
  });
}


const sysMenuExport = async (ids: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/sys/menu/export', {
    method: 'POST',
    data: {ids},
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysMenuExport = async (ids: (number | string)[]) => {
  await sysMenuExport(ids).then(({data, response}) => {
    download(data, response)
  });
}


const sysMenuExportTemplate = async () => {
  return request<{
    data: string;
  }>('/api/sys/menu/export/template', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysMenuExportTemplate = async () => {
  await sysMenuExportTemplate().then(({data, response}) => {
    download(data, response)
  });
}




